package br.com.blog.acoes;

import java.io.Serializable;
import javax.faces.application.FacesMessage;
import javax.faces.validator.ValidatorException;
import org.hibernate.Session;
import org.jboss.seam.annotations.In;
import org.jboss.seam.core.ResourceBundle;
import org.jboss.seam.faces.FacesMessages;
import org.jboss.seam.international.StatusMessage;


public class ActionPadrao implements Serializable {

  
	private static final long serialVersionUID = 1L;

	@In
    protected transient Session hibernateSession;

    @In
    private FacesMessages facesMessages;

    protected void addMsg(String msg, Object... params) {
        facesMessages.add(StatusMessage.Severity.INFO, msg, params);
    }

    protected void addMsgBundle(String key, Object... params) {
        facesMessages.addFromResourceBundle(StatusMessage.Severity.INFO, key,
                params);
    }

    protected void addMsgSucesso() {
        facesMessages.addFromResourceBundle(StatusMessage.Severity.INFO, "salvo");
    }

    protected void addMsgErro(String msg, Object... params) {
        facesMessages.add(StatusMessage.Severity.ERROR, msg, params);
    }

    protected void addMsgErroBundle(String key, Object... params) {
        facesMessages.addFromResourceBundle(StatusMessage.Severity.ERROR, key,
                params);
    }

    protected void lancarErroValidacao(String chave) throws ValidatorException {
        FacesMessage facesMessage = new FacesMessage(getMsg(chave));
        facesMessage.setSeverity(FacesMessage.SEVERITY_ERROR);
        throw new ValidatorException(facesMessage);
    }

    private String getMsg(String chave) {
        return ResourceBundle.instance().getString(chave);
    }
}
